from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from tucao.models import Tucao, Image, Comment, TucaoFollow, Collections, CommentFollow
from tucao.search_indexes import TucaoIndex


class UserRelateField(serializers.RelatedField):
    """自定义用于处理用户的字段"""

    def to_representation(self, value):
        if value:
            return {
                'user_id': value.id,
                'username': value.username,
                'nick_name': value.nick_name,
                'avatar_url': value.avatar_url,
            }
        else:
            return ''


class ImageSerializer(serializers.ModelSerializer):
    """图片序列化器"""

    class Meta:
        model = Image
        fields = ('id', 'image')


class TucaoSerializer(serializers.ModelSerializer):
    """吐槽序列化器"""
    user = UserRelateField(read_only=True)
    images = ImageSerializer(many=True)

    class Meta:
        model = Tucao
        fields = (
            'id', 'content', 'user', 'username', 'follow_count', 'comment_count', 'images', 'create_time')
        read_only_fields = ('follow_count', 'comment_count')


class TucaoDetailSerializer(serializers.ModelSerializer):
    """吐槽详情序列化器"""
    user = UserRelateField(read_only=True)
    images = ImageSerializer(many=True)

    class Meta:
        model = Tucao
        fields = (
            'id', 'content', 'user', 'follow_count', 'comment_count', 'share_count', 'username', 'images',
            'create_time')
        read_only_fields = ('follow_count', 'comment_count')


class ParentCommentSerializer(serializers.ModelSerializer):
    """父评论序列化器"""
    user = UserRelateField(read_only=True)

    class Meta:
        model = Comment
        fields = ('id', 'content', 'user', 'count', 'username')


class CommentSerializer(serializers.ModelSerializer):
    """评论序列化器"""
    parent = ParentCommentSerializer()
    user = UserRelateField(read_only=True)

    class Meta:
        model = Comment
        fields = ('id', 'content', 'user', 'count', 'username', 'parent')


class TucaoFollowSerializer(serializers.ModelSerializer):
    class Meta:
        model = TucaoFollow
        fields = ('id', 'tucao', 'user')


class TucaoSelectSerializer(serializers.ModelSerializer):
    class Meta:
        model = Collections
        fields = ('id', 'tucao', 'user')


class CommentFollowSerializer(serializers.ModelSerializer):
    class Meta:
        model = CommentFollow
        fields = ('id', 'tucao', 'user', 'comment')


class HotTucaoSerializer(serializers.ModelSerializer):
    """吐槽序列化器"""

    class Meta:
        model = Tucao
        fields = (
            'id', 'content', 'user', 'username', 'follow_count', 'comment_count', 'create_time')
        read_only_fields = ('follow_count', 'comment_count')


class TucaoIndexSerializer(HaystackSerializer):
    """
    索引结果数据序列化器
    """

    class Meta:
        index_classes = [TucaoIndex]
        fields = ('text', 'id', 'user', 'content', 'create_time', 'comment_count', 'follow_count')
